package sample;

import information.MyUrlDispose;
import information.UpInformation;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import javax.management.Query;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuKeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.ResourceBundle;

/**
 *
 */
public class BiliController implements Initializable {
    // 需要初始化的背景和图片
    @FXML
    private ImageView initializeBackground;
    @FXML
    private ImageView initializeBigBorder;
    @FXML
    private ImageView initializeSmallBorder1;
    @FXML
    private ImageView initializeSmallBorder2;
    @FXML
    private ImageView initializeSmallBorder3;

    // 头像外框
    @FXML
    private ImageView initializeCircleBorder;

    // UID
    @FXML
    private Label labelMID;

    // 粉丝数
    @FXML
    private Label labelFollower;

    // 关注数
    @FXML
    private Label labelFollowing;

    // name
    @FXML
    private Label labelName;

    // 个人简介
    @FXML
    private Label labelSign;

    // 头像
    @FXML
    private ImageView imageFace;

    // bilibili的LOGO
    @FXML
    private ImageView BiliBiliLogo;

    // 查询窗口
    @FXML
    private AnchorPane queryWindow;

    // 查询窗口的text组件
    @FXML
    private TextField uidText;

    // 查询窗口错误的提示
    @FXML
    private Label prompt;

    // 右键菜单中的退出按钮
    @FXML
    private MenuItem exitWindow;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        // 初始化所有背景
        initializeBackground();
        // 设置头像圆角边框
        Component.setFaceRadius(imageFace);

        // 初始化查询弹窗不显示
        Component.setQueryWindowStyle(queryWindow);
        queryWindow.setVisible(false);
        // 设置多行文本框在边界处换行
        labelSign.setWrapText(true);
    }

    /**
     * 初始化所有背景
     */
    private void initializeBackground()  {
        String[] pathname = {"背景1.png", "背景2-带阴影内边框.png","BiliBiliLOGO.png" , "文字外框-短.png", "头像1-外框.png"};
        for (int i = 0;i < pathname.length;i++) {
            // 加载背景、大边框、文字外框和头像外框
            File file = new File(".\\image\\"+pathname[i]);
            Image image = new Image(file.toURI().toString());
            switch (i) {
                case 0: initializeBackground.setImage(image);break;
                case 1: initializeBigBorder.setImage(image);break;
                case 2: BiliBiliLogo.setImage(image);break;
                case 3: {
                    initializeSmallBorder1.setImage(image);
                    initializeSmallBorder2.setImage(image);
                    initializeSmallBorder3.setImage(image);
                    break;
                }
                default: initializeCircleBorder.setImage(image);break;
            }

        }
    }

    /**
     * 弹出查询窗口
     */
    public void appearQueryWindow(ActionEvent event){
        queryWindow.setVisible(true);
    }

    /**
     * 当检测到确定按钮按下，获取text中的字符串
     * @param event
     */
    String uid = null;
    public void setQueryWindowData(ActionEvent event){
        // 获取text 中的字符串
        uid = uidText.getText();
        MyUrlDispose my = new MyUrlDispose();
        UpInformation up = UpInformation.getUpInformation();
        try {
            // 调用算法获取到json中的数据
            my.sumData(up, uid);
            // 将mid参数写进label面板
            labelMID.setText(up.getJsonObject("mid"));
            // 将follower参数写进label面板
            labelFollower.setText(up.getJsonObject("follower"));
            // 将following参数写进label面板
            labelFollowing.setText(up.getJsonObject("following"));
            // 将name参数写进label面板
            labelName.setText(up.getJsonObject("name"));
            // 将sign参数写进label面板
            labelSign.setText(up.getJsonObject("sign"));
            // 再次调用创建头像方法，达到刷新头像的效果
            Component.setFaceRadius(imageFace);
            // 将查询窗口隐藏
            queryWindow.setVisible(false);
        } catch (Exception e) {
            prompt.setText("UID输入有误或接口被暂时禁封");
        }

    }

    /**
     * 查询窗口中的取消按钮
     * 单击隐藏查询窗口
     * @param event
     */
    public void setCancelButton(ActionEvent event) {
        queryWindow.setVisible(false);
    }

    /**
     * 设置刷新按钮
     * @param event
     */
    public void setRefreshButton(ActionEvent event){
        setQueryWindowData(null);
    }


    /**
     * 右键菜单退出按钮
     * @param event
     */
    public void setExitWindow(ActionEvent event) {
        System.exit(0);
    }

    /**
     * 将获取到的uid输出到文件
     */
//    private boolean outputUidFile(String uid){
//        // 将输入的uid用io流输出到文件
//        File uidPath = new File(".\\UID.txt");
//        try {
//            FileOutputStream output = new FileOutputStream(uidPath);
//            output.write();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//
//
//
//    }

}


